package zjh_20230329;

import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.cj.jdbc.MysqlDataSource;
import kfm.model.Account;
import kfm.model.CateGory;
import kfm.model.Goods;
import kfm.model.Level;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

public class Demo01 {
    public static void main(String[] args) throws SQLException, InterruptedException {
        Properties properties = new Properties();
        try {
            InputStream resourceAsStream = Demo01.class.getResourceAsStream("db.properties");
            properties.load(resourceAsStream);
//            Class.forName(properties.getProperty("jdbc.driver"));
        } catch (IOException e) {
            e.printStackTrace();
        }
//        catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }

//        MysqlDataSource mysqlDataSource = new MysqlDataSource();

        //德罗伊
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(properties.getProperty("jdbc.driver"));
        druidDataSource.setUrl(properties.getProperty("jdbc.url"));
        druidDataSource.setUsername(properties.getProperty("jdbc.user"));
        druidDataSource.setPassword(properties.getProperty("jdbc.password"));

//        mysqlDataSource.setURL(properties.getProperty("jdbc.url"));
//        mysqlDataSource.setUser(properties.getProperty("jdbc.user"));
//        mysqlDataSource.setPassword(properties.getProperty("jdbc.password"));


//        QueryRunner queryRunner = new QueryRunner(mysqlDataSource);

        //连接池
        druidDataSource.setMaxActive(30);
        druidDataSource.setMinIdle(6);
//        druidDataSource.wait(60000);
        druidDataSource.setValidationQuery("SELECT 1");

        QueryRunner queryRunner = new QueryRunner(druidDataSource);
        String sql = "SELECT * FROM category";
        List<CateGory> query = queryRunner.query(sql, new BeanListHandler<>(CateGory.class));
        System.out.println(query);
    }
}